package com.作业.Day15;

import java.io.*;

public class Homework2 {
    public static void main(String[] args) throws IOException {
        File file = new File("text2.txt");
        String[] strings;
        try (FileReader fileReader = new FileReader(file);
             BufferedReader bufferedReader = new BufferedReader(fileReader)
        ){
               String line=null;
            strings = new String[9];
             int i=0;
            while ((line=bufferedReader.readLine())!=null){
                 strings[i]=line;
                 i++;
            }
            for (String string : strings) {
                System.out.println(string);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (int i1 = 0; i1 < strings.length-1; i1++) {
            for (int i = 0; i <strings.length-i1-1 ; i++) {
                if(strings[i].charAt(0)>strings[i+1].charAt(0)){
                    String name=strings[i];
                    strings[i]=strings[i+1];
                    strings[i+1]=name;
                }
            }
        }
        System.out.println("---------------------");
        for (String string : strings) {
            System.out.println(string);
        }


        try ( FileWriter fileWriter = new FileWriter(file);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {

            for (String string : strings) {
                bufferedWriter.write(string);
                bufferedWriter.newLine();
            }


        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }
}

